<?php
/**
 * Created by PhpStorm.
 * User: zengyuming
 * Date: 2017/7/31
 * Time: 上午11:57
 */

namespace Api\Controller;
class MchidOrderController extends CommonController{
    /*
     * 说明：轮询可疑商户号查询，当商户号表中的某个商户号超出指定交易笔数则认为可能被封
     * 该方法无返回值，直接处理
     *
     * */

    public function mchIdOrderCheck(){
      //select a.id , a.mch_channel , b.mch_id , count(*) as Unpaid_Order from zym_rotation as a left join zym_transaction as b on a.mch_channel = b.mch_type where b.pay_status = 0 and b.create_begtime >= 1501909871 and b.create_begtime <=  1501922780 group by b.mch_id;
        /*
         *
         * array(1) {
          [0] => array(4) {
            ["id"] => string(1) "1"
            ["mch_channel"] => string(11) "TonglianGzh"
            ["mch_id"] => string(15) "375602048164873"
            ["unpaid_order"] => string(1) "2"
          }
    }
         *
         *
         * */
        $fen = 22222;
        $NowTime = time();
        $StartTime = $NowTime - ($fen*60);
        $where = array(
            'b.pay_status' => 0,
            'b.create_begtime' => array(
                array('EGT' , $StartTime),
                array('ELT' , $NowTime),
            )
        );
        $res = M("rotation")->field('a.id,a.mch_channel,b.mch_id,count(*) as Unpaid_Order')
            ->alias('a')
            ->join('left join zym_transaction as b on a.mch_channel = b.mch_type')
            ->where($where)
            ->group('b.mch_id')
            ->select();

        foreach($res as $value){
            if($value['unpaid_order'] > 15){
                $mch_id = $value['mch_id'];
                $channel_type = $value['mch_channel'];
                // 触发条件
                $Model = M("mch_data");
                // 更新商户号表状态，不在分配该商户号
                @$Model->where(array('mch_id' => $mch_id , 'pay_type' => $channel_type))->save(array('status' => 1));

                $mchConf = $Model->field('CumulativePrice,penNum')->where(array('mch_id' => $mch_id , 'pay_type' => $channel_type))->find();
                $last_transactionTime = M("Transaction")->where(array('mch_id' => $mch_id , 'mch_type' => $channel_type))->order('id desc')->limit(1)->getField('update_begtime');

                // 加入异常商户表
                $yichangData = array(
                    'mch_uid' => $mchConf['id'],
                    'last_transactionTime' => $last_transactionTime,
                    'total_penNum' => $mchConf['penNum'],
                    'total_price' => $mchConf['CumulativePrice'],
                    'create_time' => time()
                );
                @M("abnormalmchId")->add($yichangData);

            }
        }
        dump($res);die;
    }
}